package com.syxbbs.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

//专门用于返回类,包括code，message和data字段
//T在编译的时候会被编译成object类型
public class AppResult<T> {
    //加上该注解不管什么都会返回序列化。
    /*
    *
    * */
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    //不需要返回数据的时候
    public AppResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    //静态方法
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());

    }

    //返回data版本时
    public static <T> AppResult success(T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);

    }

    public static <T> AppResult success(String message,T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), message,data);

    }
    /*
    * 失败
    * */
    public static <T> AppResult failure(T data){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(),data);

    }
    public static <T> AppResult failure(){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());

    }
    public static <T> AppResult failure(String message,T data){
        return new AppResult(ResultCode.FAILED.getCode(),message,data);


    }

    public static <T> AppResult failure(String message){
        return new AppResult(ResultCode.FAILED.getCode(),message);


    }

    public static <T> AppResult failure(ResultCode resultCode){
        return new AppResult(resultCode.getCode(),resultCode.getMessage());


    }

}
